/*
 * (C) Copyright 2011 - 2012 Samsung Electronics
 * EXT4 filesystem implementation in Uboot by
 * Uma Shankar <uma.shankar@samsung.com>
 * Manjunatha C Achar <a.manjunatha@samsung.com>
 *
 * Ext4fs support
 * made from existing cmd_ext2.c file of Uboot
 *
 * (C) Copyright 2004
 * esd gmbh <www.esd-electronics.com>
 * Reinhard Arlt <reinhard.arlt@esd-electronics.com>
 *
 * made from cmd_reiserfs by
 *
 * (C) Copyright 2003 - 2004
 * Sysgo Real-Time Solutions, AG <www.elinos.com>
 * Pavel Bartusek <pba@sysgo.com>
 *
 * SPDX-License-Identifier:	GPL-2.0+
 */

/*
 * Changelog:
 *	0.1 - Newly created file for ext4fs support. Taken from cmd_ext2.c
 *	        file in uboot. Added ext4fs ls load and write support.
 */

#include <common.h>
#include <part.h>
#include <config.h>
#include <command.h>
#include <image.h>
#include <linux/ctype.h>
#include <asm/byteorder.h>
#include <ext4fs.h>
#include <linux/stat.h>
#include <malloc.h>
#include <fs.h>


int do_ext4_writeFile(cmd_tbl_t *cmdtp, int flag, int argc,
		  char *const argv[])
{
	int ret = 0;
	const char *filename;
	char buf[100];

	if (argc < 5 || argc > 6)
		return CMD_RET_USAGE;

	if (fs_set_blk_dev(argv[1], argv[2], FS_TYPE_EXT))
		return 1;

	filename = argv[3];
	memset(buf,0,sizeof(buf));
	strncpy(buf,argv[4],strlen(argv[4]));
	printf("filename -> %s , buf -> %s , len -> %d \n", filename,buf,strlen(buf));

	ret = ext4_fs_write(filename,buf,strlen(buf));
	if (ret < 0)
		return 1;

	return ret;
}

int do_fat_writeFile(cmd_tbl_t *cmdtp, int flag, int argc,
		  char *const argv[])
{
	loff_t size;
	int ret;
	unsigned long addr;
	unsigned long count;
	block_dev_desc_t *dev_desc = NULL;
	disk_partition_t info;
	int dev = 0;
	int part = 1;
	void *buf;

	if (argc < 5)
		return 1;

	part = get_device_and_partition(argv[1], argv[2], &dev_desc, &info, 1);
	if (part < 0)
		return 1;

	dev = dev_desc->dev;

	if (fat_set_blk_dev(dev_desc, &info) != 0) {
		printf("\n** Unable to use %s %d:%d for fatwrite **\n",
			argv[1], dev, part);
		return 1;
	}

	printf("filename -> %s , buf -> %s , len -> %d \n", argv[3],argv[4],strlen(argv[4]));
	ret = file_fat_write(argv[3], argv[4], 0, strlen(argv[4]), &size);
	if (ret < 0) {
		printf("\n** Unable to write \"%s\" from %s %d:%d **\n",
			argv[3], argv[1], dev, part);
		return 1;
	}
	printf("%llu bytes written\n", size);
	return ret;
}

U_BOOT_CMD(ext4WriteFile, 6, 1, do_ext4_writeFile,
	   "write buff into Ext4 filesystem",
	   "<interface> <dev[:part]> <absolute filename path> <String>");

U_BOOT_CMD(fatWriteFile, 6, 1, do_fat_writeFile,
		"write buff into Fat filesystem",
		"<interface> <dev[:part]> <absolute filename path> <String>");

